<template>
  <div >
    <a-input v-model:value="userNames" readOnly
             :style="{cursor:notCheck?'not-allowed':''}"
             :placeholder="'请选择'+title"
             @click="onSearchDepUser"
    >
      <a-icon slot="prefix" type="team" :title="'请选择'+title"/>
      <a-icon v-show="userNames"
              @mouseover="mouse(true)"
              @mouseleave="mouse(false)"
              title="清空" slot="suffix"
              @click="removeSelect"
              :style="{color:colse?'red':'#D0D0D0',fontSize:'12px'}"
              type="close-circle"/>
    </a-input>


    <j-select-user-modal-work
      ref="selectModal"
      :showIcon="false"
      :identity="identity"
      :org-code="orgCode"
      :my-bm="myBm"
      :org-id="orgId"
      :title="title"
      :selectMy="selectMy"
      :yx="yx"
      :user-type="userType"
      :modal-width="modalWidth"
      :multi="multi"
      :not-org-code="notOrgCode"
      :not-org-code-len="notOrgCodeLen"
      @ok="selectOK"
      :user-ids="value"
      :id="id"
      @initComp="initComp"/>
  </div>
</template>

<script>
  import JSelectUserModalWork from './modal/JSelectUserModalWork'

  export default {
    name: 'JSelectUser',
    components: {JSelectUserModalWork},
    props: {
      notCheck:{
        type:Boolean,
        default:false,
        required:false
      },
      modalWidth: {
        type: Number,
        default: 1250,
        required: false
      },
      value: {
        type: String,
        required: false
      },
      disabled: {
        type: Boolean,
        required: false,
        default: false
      },
      multi: {
        type: Boolean,
        default: true,
        required: false
      },
      backUser: {
        type: Boolean,
        default: false,
        required: false
      },
      title: {
        type: String,
        default: '用户',
        required: false
      },
      orgCode: {
        type: String,
        default: '',
        required: false
      },
      identity: {
        type: String,
        default: '1',
        required: false
      },
      notOrgCode: {
        type: String,
        default: '',
        required: false
      },
      notOrgCodeLen: {
        type: Number,
        default: null,
        required: false
      },
      myBm: {
        type: Boolean,
        default: true,
        required: false
      },
      orgId: {
        type: String,
        default: '',
        required: false
      },
      userType: {
        type: String,
        default: '',
        required: false
      },
      selectMy: {
        type: Boolean,
        default: false,
        required: false
      },
      //是否严选顾问 不处理
      yx:{
        type: String,
        default: '',
        required: false
      },
      id:{
        type: String,
        default: '',
        required: true
      },
    },
    data() {
      return {
        colse:false,
        userIds: "",
        userNames: ""
      }
    },
    mounted() {
      this.userIds = this.value
    },
    watch: {
      value(val) {
        this.userIds = val
      }
    },
    model: {
      prop: 'value',
      event: 'change'
    },
    methods: {
      mouse(v){
        if(!this.notCheck){
          this.colse=v;
        }
      },
      removeSelect(){
        if(!this.notCheck){
          this.selectOK('','');
          this.$refs.selectModal.close()
        }
      },
      initComp(userNames) {
        this.userNames = userNames;
      },
      //返回选中的用户信息
      backDeparInfo(){
        if(this.backUser===true){
          if(this.userIds && this.userIds.length>0){
            let arr1 = this.userIds.split(',')
            let arr2 = this.userNames.split(',')
            let info = []
            for(let i=0;i<arr1.length;i++){
              info.push({
                value: arr1[i],
                text: arr2[i]
              })
            }
            this.$emit('back', info)
          }
        }
      },
      onSearchDepUser() {
        if(!this.notCheck){
          this.$refs.selectModal.showModal()
        }
      },
      selectOK(rows, idstr) {
        console.log("当前选中用户", rows)
        console.log("当前选中用户ID", idstr)
        if (!rows) {
          this.userNames = ''
          this.userIds = ''
        } else {
          let temp = ''
          for (let item of rows) {
            temp += ',' + item.realname
          }
          this.userNames = temp.substring(1)
          this.userIds = idstr
        }
        this.$emit("change", this.userIds)
      }
    }
  }
</script>

<style scoped>

</style>